<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'

// 定义props和emits
const props = defineProps<{
  modelValue: boolean
  title?: string
  message?: string
}>()

const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'confirm': []
}>()

// 关闭弹窗
function closeModal() {
  emit('update:modelValue', false)
}

// 确认按钮点击
function handleConfirm() {
  emit('confirm')
  closeModal()
}

// 点击遮罩层关闭弹窗
function handleMaskClick(event: MouseEvent) {
  if (event.target === event.currentTarget) {
    closeModal()
  }
}

// 按ESC键关闭弹窗
function handleKeydown(event: KeyboardEvent) {
  if (event.key === 'Escape') {
    closeModal()
  }
}

onMounted(() => {
  document.addEventListener('keydown', handleKeydown)
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<template>
  <Teleport to="body">
    <Transition name="modal">
      <div
        v-if="props.modelValue"
        class="modal-mask"
        @click="handleMaskClick"
      >
        <div class="modal-container">
          <div class="modal-header">
            <h3 class="modal-title">{{ props.title || '알림' }}</h3>
            <button class="close-btn" @click="closeModal" title="닫기">
              ×
            </button>
          </div>
          <div class="modal-body">
            <p class="modal-message">{{ props.message || '코인부족으로 라이브 입장이 불가합니다' }}</p>
          </div>
          <div class="modal-footer">
            <button class="confirm-btn" @click="handleConfirm">
              단기
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9999;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(2px);
}

.modal-container {
  position: relative;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  min-width: 300px;
  max-width: 400px;
  overflow: hidden;
  animation: modalSlideIn 0.3s ease-out;
  backdrop-filter: blur(10px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #e5e7eb;
}

.modal-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
}

.close-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: #6b7280;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #f3f4f6;
  color: #374151;
}

.modal-body {
  padding: 24px;
  text-align: center;
}

.modal-message {
  margin: 0;
  font-size: 16px;
  line-height: 1.5;
  color: #374151;
}

.modal-footer {
  padding: 16px 24px 24px;
  display: flex;
  justify-content: center;
}

.confirm-btn {
  background: #374151;
  color: white;
  border: none;
  border-radius: 6px;
  padding: 10px 24px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 70px;
}

.confirm-btn:hover {
  background: #1f2937;
}

.confirm-btn:active {
  transform: translateY(1px);
}

/* 动画效果 */
.modal-enter-active, .modal-leave-active {
  transition: opacity 0.3s ease;
}

.modal-enter-from, .modal-leave-to {
  opacity: 0;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition: transform 0.3s ease;
}

.modal-enter-from .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.8) translateY(-20px);
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-container {
    margin: 20px;
    min-width: 280px;
    max-width: calc(100vw - 40px);
  }

  .modal-header {
    padding: 16px 20px 12px;
  }

  .modal-body {
    padding: 20px;
  }

  .modal-footer {
    padding: 12px 20px 20px;
  }

  .modal-message {
    font-size: 15px;
  }
}
</style>
